<?php
/**
 * User: hjd-yao
 * Date: 2019/6/12
 * Time: 17:07
 */

namespace Shop\Http\Controllers\Wap;


use App\Models\Shop\ShopCartModel;
use App\Models\Shop\ShopGoodsModel;
use App\Models\Shop\ShopGoodsSpecValueModel;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;
use Shop\Http\Controllers\BaseController;

class CartController extends BaseController
{
    public function index(Request $request,$zone=1)
    {
        $user_id = $request->input('user_id');
        $cart_goods = ShopCartModel::with(['goods'=>function($query){
            $query->select(['goods_id','goods_name','goods_images','price','agent_price']);
        }])->whereHas('goods')->where('user_id',$user_id)->where('goods_zone',$zone)->get();
        return $this->wapRender('cart.index',[
            'page_title'=>'购物车',
            'zone'=>$zone,
            'cart_goods'=>$cart_goods,

        ]);
    }

    /**
     * @title 添加购物车
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $buy_num = abs($request->input('buy_number',1));
        $specs = $request->input('goods_specs');
        $goods_id = $request->input('goods_id');
        $goods_zone =  $request->input('goods_zone');
        $user_id = $request->input('user_id');

        // 查看 是否 有 sku 信息
        if($specs){
            $goods = ShopGoodsSpecValueModel::init()->where('goods_id',$goods_id)->where('goods_spec_items',$specs)->first();
        }else{
            $goods = ShopGoodsModel::find($goods_id);
        }

        if(!$goods){
            return response()->json(['status'=>0,'msg'=>'商品信息不存在']);
        }

        if($buy_num > $goods->stock){
            return response()->json(['status'=>0,'msg'=>'商品库存不足请重新选择购买数量']);
        }

        $hasNum = ShopCartModel::init()->where('user_id',$user_id)->count();

        if($hasNum >= config('shop.cart_max')){
            return response()->json(['status'=>0,'msg'=>'购物车商品已达上限,请支付后再来购买']);
        }

        // 根据 会员等级 获取 商品价格
        $goods_price = $this->getGoodsPrice($user_id,$goods);

        $cart = ShopCartModel::init()->where('goods_id',$goods_id)->where('spec_item',$specs)->first();

        if($cart){
            $cart->num = $cart->num+$buy_num;
            $cart->price = $goods_price;
            $cart->total_price = $cart->num * $goods_price ;
        }else{
            $cart = new ShopCartModel();
            $cart->goods_id = $goods_id;
            $cart->user_id = $user_id;
            $cart->spec_item = $specs;
            $cart->num = $buy_num;
            $cart->price = $goods_price;
            $cart->total_price = $goods_price * $buy_num ;
            $cart->goods_zone = $goods_zone;
        }

        if($cart->save()){
            return response()->json(['status'=>1,'msg'=>'添加成功']);
        }
        return response()->json(['status'=>0,'msg'=>'添加失败']);
    }

    public function update(Request $request,$cart_id)
    {
        $user_id = $request->input('user_id') ;
        $cart = ShopCartModel::where('user_id',$user_id)->find($cart_id);

        if(!$cart){
            return response()->json(['status'=>0,'msg'=>'购物车无该商品']);
        }

        // 查看 是否 有 sku 信息
        if($cart->spec_item){
            $goods = ShopGoodsSpecValueModel::init()->where('goods_id',$cart->goods_id)->where('goods_spec_items',$cart->spec_item)->first();
        }else{
            $goods = ShopGoodsModel::find($cart->goods_id);
        }

        if(!$goods){
            return response()->json(['status'=>0,'msg'=>'商品信息不存在']);
        }

        $number = $request->post('number',1);
        $number = abs(intval($number));

        if($number > $goods->stock){
            return response()->json(['status'=>0,'msg'=>'商品库存不足请重新选择购买数量']);
        }

        $cart->num = $number;
        $cart->total_price = $cart->num * $cart->price ;

        if($cart->save()){
            return response()->json(['status'=>1,'msg'=>'更新成功']);
        }
        return response()->json(['status'=>0,'msg'=>'更新失败']);
    }

    /**
     * @title 删除购物车中的商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $cart_id = array_filter(str2arr($request->input('cart_id')));

        if(!$cart_id){
            return response()->json(['status'=>0,'msg'=>'请选择要删除的商品']);
        }

        if(ShopCartModel::where('user_id',$request->input('user_id'))->whereIn('id',$cart_id)->delete()){
            return response()->json(['status'=>1,'msg'=>'删除成功']);
        }
        return response()->json(['status'=>0,'msg'=>'删除失败']);
    }


    /**
     * @title 清空购物车
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function clearAll(Request $request)
    {
        if(ShopCartModel::where('user_id',$request->input('user_id'))->delete()){
            return response()->json(['status'=>1,'msg'=>'清空购物车成功']);
        }
        return response()->json(['status'=>0,'msg'=>'清空购物车失败']);
    }


    public function getGoodsPrice($user_id,$goods){
        $level = UserBaseModel::init()->where('member_id',$user_id)->value('level');
        if($level > 0){
            $goods_price = $goods->vip_price ;
        }else{
            $goods_price = $goods->price ;
        }
       return $goods_price ;
    }

}